class SysSettingPanle extends egret.DisplayObjectContainer {
    private speed = .2;
    private context: any;
    private sound_start;
    private sound_bw;
    private sound_end;
    public constructor(context: any) {
        super();
        this.context = context;
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {
        new Bg(this, 'mask_in_png');
        var percentage = 0.7;
        //加载主面板
        var imgCon = [];
        imgCon['url'] = 'sys_set_bg_png';
        imgCon['width'] = this.stage.stageWidth * percentage * 1;
        imgCon['height'] = this.stage.stageHeight * percentage;
        imgCon['y'] = this.stage.stageHeight * (1 - percentage) / 2;
        imgCon['x'] = (this.stage.stageWidth - imgCon['width']) / 2;
        this.addChild(new ImageBitmap(imgCon).getImgPoint());
        this.setClose();
        this.switchDisplay();
        this.createSoundScence();

        this.alpha = 0;
        this.openAm();
    }

    private setClose(): void {
        /**
         * 设置关闭按钮
         * @type {eui.Button}
         */
        var close_btn = new eui.Button();
        ///设置按钮的标签
        close_btn.skinName = "close_buttom";
        close_btn.width = 60;
        close_btn.height = close_btn.width;
        close_btn.x = this.stage.stageWidth * 0.81;
        close_btn.y = this.stage.stageHeight * 0.12;
        this.addChild(close_btn);
        close_btn.touchEnabled = true;
        close_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClose, this);
    }


    private switchDisplay(): void {

        var switchX = 415;
        // var switchX = 250;
        var switchY = 0;


        // 设置游戏音乐
        switchY = this.stage.stageHeight * 0.43;
        var openFn = function () {
            this.soundctr.openGameSound();
        }
        var closeFn = function () {
            this.soundctr.closeGameSound();
        }
//      console.log(this.context.soundctr.isGameSound);
        if (this.context.soundctr.isGameSound) {
            var gameMusic = new SwitchBtu(this.context, 1, openFn, closeFn).getPoint(switchX, switchY);
        } else {
            var gameMusic = new SwitchBtu(this.context, 0, openFn, closeFn).getPoint(switchX, switchY);
        }

        this.addChild(gameMusic);

        // 设置背景音乐
        switchY = this.stage.stageHeight * 0.52;
        var openFn = function () {
            this.soundctr.playBgm();
        }
        var closeFn = function () {
            this.soundctr.closeBgm();
        }
        if (this.context.soundctr.isBgmSound) {
            var bgmMusic = new SwitchBtu(this.context, 1, openFn, closeFn).getPoint(switchX, switchY);
        } else {
            var bgmMusic = new SwitchBtu(this.context, 0, openFn, closeFn).getPoint(switchX, switchY);
        }
        this.addChild(bgmMusic);

        // 显示活动
        switchY = this.stage.stageHeight * 0.61;
        var openFn = function () {
            PreferenceData.setPromotionOpen(1);
        }
        var closeFn = function () {
            PreferenceData.setPromotionOpen(0);
        }
        this.addChild(new SwitchBtu(this.context, PreferenceData.getPromotionOpen(),openFn,closeFn).getPoint(switchX, switchY));

        //快速玩法

        switchY = this.stage.stageHeight * 0.70;
        var openFn = function () {
            PreferenceData.setFastSwitch(1);
        }
        var closeFn = function () {
            PreferenceData.setFastSwitch(0);
        }

        var speedWay = new SwitchBtu(this.context, PreferenceData.getFastSwitch(), openFn, closeFn).getPoint(switchX, switchY);
        this.addChild(speedWay);
    }

    private createSoundScence(): void {
        /**
         * 音量的显示组件
         */
        var soundDisplay = new eui.Group();
        var sHeight = 32;
        var img_start = this.sound_start = new eui.Image();
        img_start.source = "sound_start_png";
        img_start.width = 14;
        img_start.height = sHeight;
        soundDisplay.addChild(img_start);

        var img_bw = this.sound_bw = new eui.Image();
        img_bw.source = "sound_bw_png";
        img_bw.width = 400 * this.context.soundctr.volume;
        img_bw.height = sHeight;
        img_bw.x = img_start.width;
        soundDisplay.addChild(img_bw);

        var img_end = this.sound_end = new eui.Image();
        img_end.source = "sound_end_png";
        img_end.width = 50;
        img_end.height = img_end.width;
        img_end.x = img_start.width + img_bw.width - 20;
        img_end.y = -7;
        soundDisplay.addChild(img_end);

        this.addChild(soundDisplay);
        soundDisplay.x = 420;
        soundDisplay.y = 207.5;

        var exml =
            `<e:Skin minWidth="20" minHeight="8"  xmlns:e="http://ns.egret.com/eui">
                <e:Image id="track" source="`+SystemManger.RESURL+`assets/Slider/track.png" scale9Grid="1,1,4,4" width="100%" height="6" verticalCenter="0"/>
                <e:Image id="trackHighlight" source="`+SystemManger.RESURL+`assets/Slider/tracklight.png" scale9Grid="1,1,4,4" height="6" verticalCenter="0"/>
                <e:Image id="thumb" source="`+SystemManger.RESURL+`assets/Slider/thumb.png" verticalCenter="0"/>
            </e:Skin>`;
        /**
         * 音量控制组件调整的组件 
         */
        var hSlilder: eui.HSlider = new eui.HSlider();
        hSlilder.skinName = exml;
        hSlilder.width = 425;
        hSlilder.height = 20;
        hSlilder.maximum = 100;
        hSlilder.value = 100 * this.context.soundctr.volume;
        hSlilder.horizontalCenter = 0;
        hSlilder.verticalCenter = 0;
        hSlilder.y -= 40;
        hSlilder.alpha = 0;
        ///监听 CHANGE 事件
        hSlilder.addEventListener(egret.Event.CHANGE, this.onHSliderChange, this);
        soundDisplay.addChild(hSlilder);
    }

    /**
     * 音量控制界面
     */
    private onHSliderChange(e: egret.Event) {
        var slilder = <eui.HSlider>e.target;
        this.context.soundctr.volumeCtr(slilder.pendingValue / 100);

        this.sound_bw.width = 400 * slilder.pendingValue / 100;
        this.sound_end.x = this.sound_start.width + this.sound_bw.width - 20;
    }

    public openAm(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
        this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
    }
    public closeAm(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.closeFrameHandler, this);
        this.addEventListener(egret.Event.ENTER_FRAME, this.closeFrameHandler, this);
    }
    /**打开时逐帧运动*/
    private enterFrameHandler(event: egret.Event): void {
        if (this.alpha < 1) {
            this.alpha += this.speed;
        } else {
            this.pauseOpenAm();
        }
    }

    /**
     * 关闭时动画运动
     * @param event
     */
    private closeFrameHandler(event: egret.Event): void {
        if (this.alpha > 0) {
            this.alpha -= this.speed;
        } else {
            this.pauseOpenAm();
            if (this.parent) {
                this.parent.removeChild(this);
            }
        }
    }
    /**暂停打开时动画*/
    public pauseOpenAm(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
    }

    /**暂停关闭时的动画*/
    public pauseCloseAm(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.closeFrameHandler, this);
    }

    private onClose(evt: egret.TouchEvent) {
        this.closeAm();
    }

}